package us.icitap.commandcenter.ui.dispatcher;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import us.icitap.commandcenter.CommandCenterUI;
import us.icitap.commandcenter.business.ejb.persistence.Dispatcher;
import us.icitap.commandcenter.ui.custom.ButtonsLayout;
import us.icitap.commandcenter.ui.custom.CustButton;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class SelectDispatcherWindow extends Window{

	private DispatcherVerticalLayout dispatcherVl;
	
	public SelectDispatcherWindow(DispatcherVerticalLayout dispatcherVerticalLayout) {
		this.dispatcherVl = dispatcherVerticalLayout;
		
		this.setModal(true);
		this.center();
		this.setResizable(false);
		this.setCaption("Lista e shperndaresve");
		//this.setWidth("320px");
		
		
		VerticalLayout vl = new VerticalLayout();
		vl.setSizeUndefined();
		List<Dispatcher> dispatcherList = CommandCenterUI.getUser().getCommandCenter().getActiveDispatcherList();
		
		
		final OptionGroup og = new OptionGroup("Zgjidh nga lista e shperndaresve");
		og.setContainerDataSource(new BeanItemContainer<Dispatcher>(Dispatcher.class, dispatcherList));
		og.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		og.setItemCaptionPropertyId("name");
		og.setImmediate(true);
		og.setMultiSelect(true);
		
		vl.addComponent(og);
		
		CustButton selectButton = new CustButton("Hap ambjentin e punes");
		
		final ButtonsLayout buttonsLayout = new ButtonsLayout("480px");
		buttonsLayout.addComponent(selectButton);
		buttonsLayout.setComponentAlignment(selectButton, Alignment.MIDDLE_RIGHT);
		vl.addComponent(buttonsLayout);
		
		vl.setMargin(true);
		vl.setSpacing(true);
	
		selectButton.addClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				
				List<Dispatcher> list = new ArrayList<Dispatcher>();
				list.addAll((Collection<Dispatcher>) og.getValue());
				if(list.isEmpty()) return;
				dispatcherVl.setDispatchers(list);
				
				SelectDispatcherWindow.this.close();
				og.setValue(null);
			}
		});
		
		
		this.setContent(vl);
		
	}

	
	
	
}
